﻿using LPY.LeHealth.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LPY.LeHealth.DTO;
using LPY.LeHealth.DAL;

namespace LPY.LeHealth.WebSite.Controllers
{
    /// <summary>
    /// 用户后台
    /// </summary>
    public class UserController : BaseController
    {
        private UserService userService;

        public UserController()
        {
            userService = new UserService();
        }


        /// <summary>
        /// 用户中心首页
        /// </summary>
        /// <returns></returns>
        [Common.User]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(string name,string pwd)
        {
            var usr= userService.Login(name, pwd);
            if(null!=usr)
            {
                Session[CommonService.UserSessionName] = usr;
                //跳转到用户中心
                return RedirectToAction("Index");
            }

            return View();
        }

        /// <summary>
        /// 手机号码登录
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult PhoneLogin(string phone, string code, string ReturnUrl)
        {
            var data = new Common.JsonMessage();
            var sc = Session[phone].ToString();
            if (code == sc)
            {
                var usr = userService.Login(phone);
                if (null != usr)
                {

                    
                    
                    Session[CommonService.UserSessionName] = usr;
                    data.Code = 1;
                    data.Msg = "登录成功";
                    if (!string.IsNullOrEmpty(ReturnUrl))
                    {
                        data.Data = "http://" + Request.Url.Authority + "/" + ReturnUrl;
                    }

                }
                else
                {
                    data.Code = 0;
                    data.Msg = "登录失败";
                }
            }
            else
            {
                data.Code = 0;
                data.Msg = "验证码无效";
            }

            return Json(data);
        }

        
        /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(string name)
        {
            return View();
        }
        /// <summary>
        /// 修改名称
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        //public JsonResult GetByname(UserDTO user)
        //{
        //    return Json(userService.GetByname(UserDTO user.Name), JsonRequestBehavior.AllowGet);
        //}
        


        
	}
}